package com.gz.DroidPong.Scenes;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.entity.primitive.Line;
import org.anddev.andengine.entity.primitive.Rectangle;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.shape.Shape;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.extension.physics.box2d.PhysicsConnector;
import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;
import org.anddev.andengine.input.touch.TouchEvent;

import android.hardware.SensorManager;
import android.util.Log;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.gz.DroidPong.Constants.TextureManager;
import com.gz.DroidPong.Constants.UIManager;
import com.gz.DroidPong.Sprites.Cup;
import com.gz.Game.Extension.GameScene;

public class DroidPongScene extends GameScene{

	public static final int LAYER_BACKGROUND = 0;
	public static final int LAYER_SPRITES = LAYER_BACKGROUND + 1;
	public static final int LAYER_HUD = LAYER_SPRITES + 1;

	private Engine mEngine;
	private PhysicsWorld mPhysicsWorld;
	private float mGravityX,mGravityY;
	private boolean mGameLoaded = false;
	
	private Sprite mBall;
	private Body mBody;
	private Line line;
	private Vector2 mTouchLocation = new Vector2();
	
	public DroidPongScene(int pLayerCount, Engine baseEngine) {
		super(pLayerCount, baseEngine);
		// TODO Auto-generated constructor stub
		
		mEngine = baseEngine;
		setOnSceneTouchListener(getTouchListener());
		log("Init");
	}

	private boolean mTouch = false;
	
	private IOnSceneTouchListener getTouchListener(){
		return new IOnSceneTouchListener() {

			public boolean onSceneTouchEvent(Scene pScene,
					TouchEvent pSceneTouchEvent) {
				if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP){
					if(mGameLoaded){
						//mBall.setPosition(75, 100);
						ShootBall(pSceneTouchEvent);
					}
					
					mTouch = false;
				}
				else if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN){
					mTouch = true;
					mTouchLocation = new Vector2(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
				}
				else if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_MOVE){
					mTouchLocation = new Vector2(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
				}
				return true;
			}
			
		};
	}
	
	@Override
	protected void onLoadComplete() {
		log("Load complete");
	}

	@Override
	protected void onLoadResources() {
		log("Load Resources");
	}

	@Override
	protected void onLoadScene() {
		log("Load scene");
		
		mEngine.registerUpdateHandler(new FPSLogger());
		mPhysicsWorld = new PhysicsWorld(new Vector2(0,SensorManager.GRAVITY_EARTH), false);
		
		registerUpdateHandler(mPhysicsWorld);
		
		InitGame();
		mEngine.setScene(this);
		
		registerUpdateHandler(new IUpdateHandler(){

			@Override
			public void onUpdate(float pSecondsElapsed) {
				// TODO Auto-generated method stub
//				if(mBall.getY() > .75f*UIManager.getHeight() && mBody.getLinearVelocity().y < 1){
//					log("Dampening");
//					mBody.setLinearDamping(3);
//				}
//				else if(mBody.getLinearDamping() > 0)
//				{
//					mBody.setLinearDamping(0);
//				}
				
				if(mTouch)
				{
					line.setPosition(mBall.getX() + mBall.getWidth()/2, mBall.getY() + mBall.getHeight()/2, mTouchLocation.x, mTouchLocation.y);
				}
				else line.setPosition(mBall.getX() + mBall.getWidth()/2, mBall.getY() + mBall.getHeight()/2,mBall.getX() + mBall.getWidth()/2, mBall.getY() + mBall.getHeight()/2);
			}

			@Override
			public void reset() {
				// TODO Auto-generated method stub
				
			}
			
		});
		
	}

	@Override
	protected void unloadScene() {

	}

	@Override
	protected void onManagedDraw(final GL10 pGL, final Camera pCamera) {
		try {
			
				super.onManagedDraw(pGL, pCamera);
			
		} catch (Exception e) {
			Log.e("EuchreGameScene", e.getMessage());
		}
	}
	
	private void InitGame(){
		InitStaticObjects();
		Sprite grey = AddBall();
		mGameLoaded = true;
		line = new Line(10,10,20,20);
		this.getChild(LAYER_HUD).attachChild(line);
	}
	
	private void InitStaticObjects(){
		// Create Walls
		float w = 5;
		final Shape ground = new Rectangle(0, UIManager.getHeight() - w, UIManager.getWidth(), w);
		final Shape roof = new Rectangle(0, 0, UIManager.getWidth(), w);
		final Shape left = new Rectangle(0, 0, w, UIManager.getHeight());
		final Shape right = new Rectangle(UIManager.getWidth() - w, 0, w, UIManager.getHeight());
		ground.setColor(0, 1, 0);
		roof.setColor(0, .2f, .5f);
		final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(100, 0.5f, 0.72f);
		final FixtureDef groundFixtureDef = PhysicsFactory.createFixtureDef(100, 0.3f, 1f);
		final FixtureDef roofFixtureDef = PhysicsFactory.createFixtureDef(100, 0.5f, 0.72f);
		PhysicsFactory.createBoxBody(this.mPhysicsWorld, ground, BodyType.StaticBody, groundFixtureDef);
		PhysicsFactory.createBoxBody(this.mPhysicsWorld, roof, BodyType.StaticBody, roofFixtureDef);
		PhysicsFactory.createBoxBody(this.mPhysicsWorld, left, BodyType.StaticBody, wallFixtureDef);
		PhysicsFactory.createBoxBody(this.mPhysicsWorld, right, BodyType.StaticBody, wallFixtureDef);

		this.getChild(LAYER_SPRITES).attachChild(ground);
		this.getChild(LAYER_SPRITES).attachChild(roof);
		this.getChild(LAYER_SPRITES).attachChild(left);
		this.getChild(LAYER_SPRITES).attachChild(right);
		
		//Create Pedestal
//		final float pedHeight = 60;
//		final float vertPedWidth = 10;
//		final float pedWidth = 100;
//		final float pedLeftX = (vertPedWidth/2) +(2*UIManager.getWidth()/3)-(pedWidth/2);
//		final float pedY = UIManager.getHeight()- pedHeight;
//		final Shape vertPole = new Rectangle(2*UIManager.getWidth()/3,UIManager.getHeight() - pedHeight,vertPedWidth,pedHeight);
//		final Shape horPed = new Rectangle(pedLeftX,pedY,pedWidth,vertPedWidth);
//		final FixtureDef pedFixtureDef = PhysicsFactory.createFixtureDef(0, 0.7f, 0.2f);
//		PhysicsFactory.createBoxBody(this.mPhysicsWorld, vertPole, BodyType.StaticBody, pedFixtureDef);
//		PhysicsFactory.createBoxBody(this.mPhysicsWorld, horPed, BodyType.StaticBody, pedFixtureDef);
//
//		final Shape pad = new Rectangle(25,100,pedWidth,vertPedWidth);
//		final FixtureDef padFixtureDef = PhysicsFactory.createFixtureDef(0, 0.0f, 1f);
//		PhysicsFactory.createBoxBody(this.mPhysicsWorld, pad, BodyType.StaticBody, padFixtureDef);
//		
//		this.getChild(LAYER_SPRITES).attachChild(vertPole);
//		this.getChild(LAYER_SPRITES).attachChild(horPed);
		Cup cup = new Cup(300,250,50,mPhysicsWorld, this, LAYER_SPRITES);
		
	}
	
	private Sprite AddBall(){
		mBall = new Sprite(100,100,UIManager.getBallSize(),UIManager.getBallSize(),UIManager.getTextures().getTexture(TextureManager.WHITE_BALL_TEXTURE));
		
		final FixtureDef ballFD = PhysicsFactory.createFixtureDef(.084f, 0.25f, 0.75f);
		
		new PhysicsFactory();
		mBody = PhysicsFactory.createCircleBody(mPhysicsWorld, mBall, BodyType.DynamicBody, ballFD);
		
		mBody.setLinearDamping(0);
		
		mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(mBall, mBody));
		
		this.getChild(LAYER_SPRITES).attachChild(mBall);
		
		return mBall;
	}

	private void ShootBall(TouchEvent pSceneTouchEvent){
		float maxSpeed = 100;
		Vector2 vel = new Vector2(pSceneTouchEvent.getX() - (mBall.getX() - mBall.getWidth()/2), pSceneTouchEvent.getY() - (mBall.getY() - mBall.getHeight()/2));
		
		float ratio = vel.x/vel.y;
		
		if(ratio < 0)ratio = -ratio;
		
		if(ratio > 1 || ratio < -1){
			if(vel.x > 0)vel.x = 100;
			else vel.x = -100;
			
			if(vel.y > 0)vel.y = 100/ratio;
			else vel.y = -100/ratio;
		}
		else
		{
			if(vel.y > 0)vel.y = 100;
			else vel.y = -100;
			
			if(vel.x > 0)vel.x = 100*ratio;
			else vel.x = -100*ratio;
		}
		
		mBody.setLinearVelocity(vel);
		//mBody.applyForce(new Vector2(0,100), mBody.getWorldCenter());
	}
	
	private void log(String msg){
		Log.d("PongScene", msg);
	}
	
}
